const http = require('http');
const url = require('url');
const userData = require('../data/user.js');
const server = http.createServer((req, res) => {
	let path, get, post;
	console.log(req.method)
	if (req.method === 'GET') {
		let {
			pathname,
			query
		} = url.parse(req.url, true);
		path = pathname;
		get = query;
		complete();
	} else if (req.method === 'POST') {
		let arr = [];
		path = req.url;
		req.on('data', buffer => {
			arr.push(buffer)
		})
		req.on('end', () => {
			post = querystring.parse(Buffer.concat(arr).toString())
		})
		complete()
	}
	// 我的--header用户卡券余额等信息
	function complete() {
		if (path === '/user/count') {
			res.writeHead(200, {
				"Content-Type": 'application/json'
			})
			res.end(JSON.stringify(userData.data.userCount))
		} else if (path === '/home/bannerImage') {
			res.writeHead(200, {
				"Content-Type": 'application/json'
			})
			res.end(JSON.stringify(userData.data.homeImgs))
		} else if (path === '/home/adsGoods') {
			res.writeHead(200, {
				"Content-Type": 'application/json'
			})
			res.end(JSON.stringify(userData.data.adsGoods))
			// 首页tab商品数据
		} else if (path === '/home/tabGoods') {
			res.writeHead(200, {
				"Content-Type": 'application/json'
			})
			const {
				type
			} = get;
			console.log(type)
			if (Number(type) === 1) {
				res.end(JSON.stringify(userData.data.tab1Goods))
			} else if (Number(type) === 2) {
				res.end(JSON.stringify(userData.data.tab2Goods))
			} else if (Number(type) === 3) {
				res.end(JSON.stringify(userData.data.tab3Goods))
			} else if (Number(type) === 4) {
				res.end(JSON.stringify(userData.data.tab4Goods))
			}
		}
	}
})
server.listen(3000, () => {
	console.log('server已启动')
})
